/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.chipchip.chipchipserver;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import zme.api.core.Environment;
import zme.api.graph.*;
import zme.api.oauth.*;
import zme.api.exception.ZingMeApiException;

/**
 *
 * @author Tri
 */
public class ZingMeService {
    private static ZingMeService instance = null;
    public static ZingMeService sharedInstance() {
        if(instance == null) {
            instance = new ZingMeService();
        }
        return instance;
    }
    private String defaultSignedRequest = "hDkp36mUzXk2ASrXSKK92a75PrMv8AtLBp4MqUKp7nM=.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjcyMDAsImlzc3VlZF9hdCI6MTM4ODgyNjgxMCwiYWNjZXNzX3Rva2VuIjoiZDIzMTk3MWJlOGYzNDdlMmIyYTA1ZDNlMmRiZTRjOTQuWW1Ga1l6QTVZVEE9TmlRMTh1RFpkMmpTeVZqZ3Y2czQ4M1VudkxvTDNCS0hUajNVRERQZ1pJbmtrVXFtdEp0T1EwNjZ0bTcwRXp2Skc5M21CcGR4M0czdzdKaVl6dXdidG84anlRUTNzRnNGR3QyOGZVSUtXZU9KQlZGWWt6LUVkNmpxWTlNanhEdDhCbWMzbndZSnNLNlA5aFMxIiwidWlkIjo2OTgxNjA4Mn0=";
    private ZME_Authentication appAuthentication;
    private ZME_Me zingME;
    private String accessToken = "";
    
    private ZingMeService() {
        appAuthentication = new ZME_Authentication(AppConfig.CONST_APP_NAME, AppConfig.CONST_APP_API_KEY, AppConfig.CONST_APP_SECRET_KEY, Environment.DEVELOPMENT);
    }
    /**
     * 
     * @param authorizationCode authorizationCode được cung cấp từ app. AuthorizationCode tương tự như sau: hGaGcVsxU1NDFmo5mViNQiyV1eR8qaLYbJzNef6iJHcp0axNWiHUDjLHIzELepXSq30uweF-BWc1PY_ww81g6kqRTV6zXJH2xaubfEhUOXVcG2EYkTypIzeJG_34md5LsHrMu8xE5bkx4cgFfT0h2RDs28hqt2jNuY48JHf0OvvGaXgG3FNy5sqFtE4EvAeHBpdPja_Bg4XwJC6oLionCIipiF8opR8YNbac1i2GIiUiE40%3D
     * @return accessToken tương ứng với authorization được cấp
     */
    public String getAccessTokenFromAuthorizationCode(String authorizationCode) throws IOException, ZingMeApiException {
        return appAuthentication.getAccessTokenFromCode(authorizationCode);
    }
    
    public String getAccessTokenFromSignedRequest(String signedRequest) throws ZingMeApiException {
        this.accessToken = appAuthentication.getAccessTokenFromSignedRequest(signedRequest);
        return this.accessToken;
    }
    
    public ZME_Me getMeFromAccessToken(String accessToken) {
        return new ZME_Me(accessToken);
    }
    
    public ZME_Me getMeFromSignedRequest(String signedRequest) throws  ZingMeApiException {
        String meAccessToken = appAuthentication.getAccessTokenFromSignedRequest(signedRequest);
        return this.getMeFromAccessToken(meAccessToken);
    }
    
    public ZME_Me getMeFromAuthorizationCode(String authorizationCode) throws ZingMeApiException, IOException {
        String meAccessToken = appAuthentication.getAccessTokenFromCode(authorizationCode);
        return this.getMeFromAccessToken(meAccessToken);
    }
    
    public ZME_Me getDefaultUser() throws ZingMeApiException {
        String meAccessToken = appAuthentication.getAccessTokenFromSignedRequest(defaultSignedRequest);
        return this.getMeFromAccessToken(meAccessToken);
    }
}
